#include<QStringList>
#include"profile.h"
#include<QSqlQuery>
#include<QDebug>
#include<QMutableListIterator>
#include"recipe.h"
#include<QTime>

bool has_ingredients(int id)
{

    if(qrand()%2)
        return true;
    else
        return false;
    QSqlQuery subQuery;
    subQuery.exec("SELECT food_id FROM Ingredient WHERE recipe_id="+QString::number(id)+";");

    QList<int> fridgeItems;
    fridgeItems.append(0);
    fridgeItems.append(345);
    fridgeItems.append(34);
    fridgeItems.append(56);
    fridgeItems.append(4);
    fridgeItems.append(44);

    bool found = false;

    while(subQuery.next())
    {
        for(int i=0;i<fridgeItems.size();i++)
        {
            if(fridgeItems.at(i) == subQuery.value(0).toInt())
            {
                found = true;
                i=fridgeItems.size();
            }
        }
        if(!found)
        {
            return false;
        }
        found = false;
    }

    return true;

}

bool has_allergies(Recipe recipe)
{
    QSqlQuery subQuery;
    subQuery.exec("SELECT food_id FROM Ingredient WHERE recipe_id="+QString::number(recipe.id)+";");

    QList<int> allergies;
    allergies.append(1);
    allergies.append(345);
    allergies.append(34);
    allergies.append(56);
    allergies.append(4);
    allergies.append(44);

    while(subQuery.next())
    {
        for(int i=0;i<allergies.size();i++)
        {
            if(allergies.at(i) == subQuery.value(0).toInt())
            {
                return true;
            }
        }
    }

    return false;
}

static QStringList search()
{
    QTime time = QTime::currentTime();
    qsrand((uint)time.msec());
    QStringList list;
    Profile profile;
    profile.vegetarian = true;
    QSqlQuery query;

    QList<Recipe*> recipes;

    //starting the filter process
    qDebug()<<"Starting the filter process";
    //FILTER 1:

    //check if vegetarian
    qDebug()<<"Checking if user is vegetarian and filtering results";
    if(profile.isVegetarian()){
        query.exec("SELECT COUNT(*) FROM Recipe WHERE vegetarian=1;");
        query.next();
        int l=query.value(0).toInt();
        query.exec("SELECT id FROM Recipe WHERE vegetarian=1;");
        qDebug()<<"User is vegetarian"<<1082-l<<"results filtered";

    }
    else{
        query.exec("SELECT id FROM Recipe;");
        qDebug()<<"User not vegetarian, no results filtered";
    }
    //FILTER 2:
    //check if available
    qDebug()<<"Checking for availability ";
    int k=0;
    while(query.next())
    {
        k++;
        if(has_ingredients(query.value(0).toInt()))
        {
            Recipe *recipe = new Recipe(query.value(0).toInt());
            recipes.append(recipe);
        }
    }
    qDebug()<<recipes.size()<<" of "<<k<<"total, ("<<k-recipes.size()<<" filtered)";
    //FILTER 3:
    //check for known allergies
    qDebug()<<"Checking for allergies";
    k=0;
    QMutableListIterator<Recipe*> i(recipes);
    while (i.hasNext()) {
        k++;
        if (has_allergies(*i.next()))
            i.remove();
    }
    qDebug()<<recipes.size()<<" of "<<k<<"total, ("<<k-recipes.size()<<" filtered)";

    //evaluate recipe
    int best_score = 0;
    QString best_name = "";
    int score= 0 ;
    for(int i=0;i<recipes.size();i++)
    {
        score = recipes.at(i)->evaluate();
        if(score>=best_score)
        {
            best_score=score;
            best_name=recipes.at(i)->name;
        }
    }
    list.append(best_name);
    return list;
}
